﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace reader.reader.accuweather
{
    public class AccuWeatherReader
    {
        private HttpRequest m_httpRequest;

        // URLs
        // http://shgao.accu-weather.com/widget/shgao/city-find.asp?location=vienna"
        // http://shgao.accu-weather.com/widget/shgao/weather-data.asp?location=SAM|CO|CO000|VIRGINIA&metric=1
        private string m_hostName;
        private string m_userName;
        private string m_page_findCity;
        private string m_page_weatherdata;
        
        // buffers
        private string m_lastCity;
        private string m_lastCityXmlString;
        private XmlDocument m_lastCityXml;

        private string m_lastLocation;
        private string m_lastLocationWeatherXmlString;
        private XmlDocument m_lastLocationWeatherXml;

        public AccuWeatherReader()
        {
            // default values
            m_hostName = "accu-weather.com";
            m_userName = "shgao";
            m_page_findCity = "city-find.asp";
            m_page_weatherdata = "weather-data.asp";

            // init buffers
            m_lastCity = "";
            m_lastCityXmlString = "";
            m_lastCityXml = new XmlDocument();

            m_lastLocation = "";
            m_lastLocationWeatherXmlString = "";
            m_lastLocationWeatherXml = new XmlDocument();

            m_httpRequest = new HttpRequest();
        }

        private string getFindCityURL(string city)
        {
            // "http://shgao.accu-weather.com/widget/shgao/city-find.asp?location=vienna"
            return "http://" + m_userName + "." + m_hostName + "/widget/" 
                    + m_userName +"/" + m_page_findCity + "?location=" + city;
        }
        private string getWeatherDataURL(string location, bool metric)
        {
            // http://shgao.accu-weather.com/widget/shgao/weather-data.asp?location=SAM|CO|CO000|VIRGINIA&metric=1
            return "http://" + m_userName + "." + m_hostName + "/widget/"
                + m_userName + "/" + m_page_weatherdata + "?location=" + location + "&metric=" + (metric?"1":"0");
        }
        public XmlDocument FindCity(string city)
        {
            if (m_lastCity != city)
            {
                m_lastCity = city;

                m_httpRequest.OpenRequest(getFindCityURL(city), null);

                m_lastCityXmlString = m_httpRequest.HtmlDocument;
                try
                {
                    m_lastCityXml.LoadXml(m_lastCityXmlString);
	            }
	            catch (XmlException e)
	            {
                    Console.WriteLine("XML loading failed " + e.StackTrace);
                    m_lastCityXml.LoadXml("<ERROR>XML loading failed</ERROR>");
	            }
            }
            Console.Write(m_lastCityXml.InnerXml);
            return m_lastCityXml;
        }
        public XmlDocument GetWeatherData(string location, bool metric)
        {
            if (m_lastLocation != location)
            {
                m_lastLocation = location;

                m_httpRequest.OpenRequest(getWeatherDataURL(location, metric), null);

                m_lastLocationWeatherXmlString = m_httpRequest.HtmlDocument;
                try
                {
                    m_lastLocationWeatherXml.LoadXml(m_lastLocationWeatherXmlString);
                }
                catch (XmlException e)
                {
                    Console.WriteLine("XML loading failed " + e.StackTrace);
                    m_lastLocationWeatherXml.LoadXml("<ERROR>XML loading failed</ERROR>");
                }
            }
            Console.Write(m_lastLocationWeatherXml.InnerXml);
            return m_lastLocationWeatherXml;
        }
    }
}
